﻿using System;
using System.Web.UI;
using System.Text;
using QzEbook.BaseView;
using QzEbook.AccessObject;
using QzEbook.Common.Emum;
using QzEbook.Admin;
using QzEbook.Common;

public partial class qzEookViewer :BasePage
{
    private Book book;
    protected void Page_Load(object sender, EventArgs e)
    {
        int bookId = int.Parse(Request["Bid"].ToString());
        
        addHit(bookId);
    }

    private void addHit(int bookId)
    {

        string sessionName = String.Format("{0}HitViewFinished", bookId);
        if (Session[sessionName] == null || !(bool)Session[sessionName])
        {
            Hit hit = new Hit();
            hit.BId = bookId;
            hit.Type = HitAction.HitView;
            hit.update();
            Session[sessionName] = true;
        }

        book = new Book();
        

        book.BookId = bookId;
        book=book.select();

        this.Title = book.Title;
    }


    public void bulidSWF()
    {
        StringBuilder sb = new StringBuilder();
        if (book.Price < 1)
        {
            outSWF(sb);
            return;
        }
        SessionUser userSession=Session[SessionManager.LOGINUSER] as SessionUser;
        if (userSession != null && userSession.Uid == book.EditorId)
        {
            outSWF(sb);
            return;
        }

        outErrorMessage(sb);

        
    }


    private void outSWF(StringBuilder sb)
    {
        sb.Append("<script language=\"javascript\" type=\"text/javascript\">");
        sb.AppendFormat("var xmlPath=\"GetConfig.aspx?Bid={0}\";", book.BookId);
        sb.AppendLine();
        sb.AppendLine("var crlnet='crlnet'");
        if (!book.ViewType)
        {
            sb.AppendFormat("var so = new SWFObject('QzReader/ipageBook.swf',crlnet , '100%', '100%', '8', '', true);");
        }
        else
        {
            sb.AppendFormat("var so = new SWFObject('QzReader/singlepage.swf',crlnet , '100%', '100%', '8', '', true);");
        }
        sb.AppendLine();

        sb.AppendFormat("so.addParam(\"FlashVars\", \"configxml=\"+xmlPath);");
        sb.AppendLine();
        sb.AppendFormat("so.addParam('allowFullScreen', '{0}');", true);
        sb.AppendLine();
        sb.AppendLine("so.setAttribute('xiRedirectUrl', window.location);");
        sb.AppendLine("so.write('flashcontent');");
        sb.Append("</script>");

        Response.Write(sb.ToString());
    }

    private void outErrorMessage(StringBuilder sb)
    {
        Response.Clear();
        Response.Write("<h3>你并未对本书进行支付.</h3>");

        sb.Append("<script language=\"javascript\" type=\"text/javascript\">");
        sb.Append("window.resizeTo(200,150);");
        sb.Append("setTimeout(closeWin,4000);");
        sb.Append("function closeWin(){window.opener='null',window.close();}");
        //sb.Append("if(confirm('你并未支付,要关闭此空口'))window.opener='null',window.close(); ");
        sb.Append("</script>");

        Response.Write(sb.ToString());
    }
}